package design_mode.代理;

import design_mode.代理.jdk.RealSubject;
import design_mode.代理.jdk.Subject;
import design_mode.代理.jdk.Subject2;
import design_mode.代理.jdk.SubjectProxy;

import java.lang.reflect.Proxy;

/**
 * JDK通过 java.lang.reflect包下面的Proxy与InvocationHandler来支持动态代理。
 */
public class ProxyMain {

    public static void main(String... args) {
        RealSubject subject = new RealSubject();
        //创建一个Invoke Handler 
        SubjectProxy proxy = new SubjectProxy(subject);
        // 通过Proxy newProxyInstance方法创建代理类

        Object o = Proxy.newProxyInstance(ProxyMain.class.getClassLoader(), subject.getClass().getInterfaces(), proxy);

        Subject realSubject = (Subject) o;
        int result = realSubject.getInt(12);
        System.out.println("result:" + result);


        Subject2 s2 = (Subject2) o;
        s2.getString("hello");
    }
}